//
//  YBNormalWaterLayoutView.m
//  GongXinDangJian
//
//  Created by apple on 17/11/30.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBNormalWaterLayoutView.h"
#import "YBWaterLayout.h"
#import "YBNormalWaterCollectionViewCell.h"
#define CELLID @"YBNormalWaterCollectionViewCell"

@interface YBNormalWaterLayoutView ()<UICollectionViewDelegate , UICollectionViewDataSource ,YBWaterLayoutDelegate>

@end

@implementation YBNormalWaterLayoutView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self) {
        _dataArray = @[];
        self.titleLabel = [ToolClass creatLabelWithFrame:CGRectMake(ItemWidth * 30, 0, [UIScreen zj_screenWith] - ItemWidth * 60, ItemHeight * 80) font:15 textColor:[UIColor textBlackColor] text:title];
        [self addSubview:self.titleLabel];
        [self ybInitMyView];
    }
    return self;
}

-(void)ybInitMyView{
    [self addSubview:self.collectionView];
    [self addSubview:self.selectButton];
    
}


-(void)deleteItemAtIndex:(NSInteger)index{
    NSMutableArray * theArray = [self.dataArray mutableCopy];
    [theArray removeObjectAtIndex:index];
    _dataArray = [theArray copy];
    self.titleLabel.text = [NSString stringWithFormat:@"发布对象(%ld):" , _dataArray.count];
    [self.collectionView reloadData];
}

-(void)option_flowLayoutCellDeleteButton:(UIButton *)sender{
    NSInteger index = sender.tag - 500;
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickDeleteButtonWithIndex:)]) {
        [self.delegate didClickDeleteButtonWithIndex:index];
    }
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YBNormalWaterCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELLID forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    cell.deleteButton.tag = 500 + indexPath.row;
    [cell.deleteButton addTarget:self action:@selector(option_flowLayoutCellDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}



-(CGFloat)WaterfallFlowLayout:(YBWaterLayout *)layout widthForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YBPeopleInfoModel * model = self.dataArray[indexPath.row];
    return model.nameWidth + ItemWidth * 60;
}


-(void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    self.titleLabel.text = [NSString stringWithFormat:@"发布对象(%ld):" , _dataArray.count];
    [self.collectionView reloadData];
}


-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        YBWaterLayout * layout = [[YBWaterLayout alloc]init];
        layout.minimumLineSpacing = ItemHeight * 1;
        layout.minimumInteritemSpacing = ItemWidth * 10;
        layout.sectionInset = UIEdgeInsetsMake(ItemHeight * 5, ItemWidth * 10, ItemHeight * 5 , ItemWidth * 10);
        layout.itemHeight = 75 * ItemHeight;
        layout.delegate = self;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(ItemWidth * 30, ItemHeight * 80, [UIScreen zj_screenWith] - ItemWidth * 60, ItemHeight * 240) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.layer.borderColor = [UIColor textGrayColor].CGColor;
        _collectionView.layer.borderWidth = normalBorderWidth;
        _collectionView.clipsToBounds = YES;
        _collectionView.layer.cornerRadius = normalCornerRadius;
        [_collectionView registerClass:[YBNormalWaterCollectionViewCell class] forCellWithReuseIdentifier:CELLID];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.allowsSelection = NO;
    }
    return _collectionView;
}


-(UIButton *)selectButton
{
    if (!_selectButton) {
        _selectButton = [ToolClass createButtonWithFrame:CGRectMake(ItemWidth * 150, ItemHeight * 355, [UIScreen zj_screenWith] - ItemWidth * 300, ItemHeight * 80) text:@"选择人员" textColor:[UIColor whiteColor] imageName:@"add_people" font:18 imageSize:ItemHeight * 35];
        _selectButton.clipsToBounds = YES;
        _selectButton.layer.cornerRadius = normalCornerRadius;
        _selectButton.backgroundColor = [UIColor themeColor];
    }
    return _selectButton;
}

@end
